﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;

//配置文件内容 转换成实体需要NuGet
//using Microsoft.Extensions.Configuration.Binder;

namespace DBUtility.Standard
{
    /// <summary>
    /// .Net Core 读取配置文件类
    /// </summary>
    public class ConfigHelper
    {
        public static IConfiguration Configuration { get; set; }

        /// <summary>
        /// appsettings.json
        /// </summary>
        /// <param name="contentPath">appsettings.json所在目录路径，不填时表示根目录路径</param>
        public ConfigHelper(string contentPath = "")
        {
            string Path = "appsettings.json";

            //如果你把配置文件 是 根据环境变量来分开了，可以这样写
            //Path = $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json";

            if (!string.IsNullOrEmpty(contentPath))
            {
                contentPath = "\\" + contentPath;
            }

            Configuration = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory() + contentPath)
                    .Add(new JsonConfigurationSource
                    {
                        Path = Path,
                        Optional = false,
                        ReloadOnChange = true
                    }) //这样的话，可以直接读目录里的json文件，而不是 bin 文件夹下的，所以不用修改复制属性
                .Build();
        }

        public ConfigHelper(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">节点拼接字符串 例如 App:Name"</param>
        /// <returns></returns>
        public IEnumerable<T> GetEnumerable<T>(string key)
        {
            try
            {
                if (Configuration != null)
                {
                    var list = (IEnumerable<T>)Configuration.GetSection(key).GetChildren().Select(child => child.Get<T>());
                    if (list != null)
                    {
                        return list;
                    }
                }
            }
            catch (Exception)
            {

            }
            return default;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">节点拼接字符串 例如 App:Name"</param>
        /// <returns></returns>
        public List<T> GetList<T>(string key)
        {
            try
            {
                var enumerable = GetEnumerable<T>(key);
                if (enumerable != null)
                {
                    var list = enumerable.ToList();
                    if (list != null)
                    {
                        return list;
                    }
                }
            }
            catch (Exception)
            {

            }
            return default;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">节点拼接字符串 例如 App:Name"</param>
        /// <returns></returns>
        public T Get<T>(string key)
        {
            try
            {
                if (Configuration != null)
                {
                    var section = Configuration.GetSection(key);

                    return section.Get<T>();
                }
            }
            catch (Exception)
            {

            }
            return default;
        }

        /// <summary>
        /// 根据路径  configuration["App:Name"];
        /// </summary>
        /// <param name="key">节点拼接字符串 例如 App:Name"</param>
        /// <returns></returns>
        public string Get(string key)
        {
            try
            {
                if (Configuration != null)
                {
                    string value = Configuration.GetSection(key).Value;
                    if(!string.IsNullOrEmpty(value))
                    {
                        return value;
                    }
                }
            }
            catch (Exception)
            {

            }
            return "";
        }

        /// <summary>
        /// 封装要操作的字符
        /// </summary>
        /// <param name="sections">节点配置</param>
        /// <returns></returns>
        public string Get(params string[] sections)
        {
            try
            {
                if (Configuration != null)
                {
                    if (sections.Any())
                    {
                        string key = string.Join(":", sections);
                        string value = Configuration.GetSection(key).Value;
                        if (!string.IsNullOrEmpty(value))
                        {
                            return value;
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
            return "";
        }
    }
}
